package com.tarena.lbs.marketing.web.repository.impl;

import com.github.pagehelper.PageInfo;
import com.tarena.lbs.base.protocol.exception.BusinessException;
import com.tarena.lbs.common.marketing.converter.CouponConverter;
import com.tarena.lbs.marketing.web.mapper.CouponMapper;
import com.tarena.lbs.marketing.web.repository.CouponCodeRepository;
import com.tarena.lbs.marketing.web.repository.CouponRepository;
import com.tarena.lbs.base.protocol.pager.BasePageList;
import com.tarena.lbs.pojo.marketing.bo.CouponBO;
import com.tarena.lbs.pojo.marketing.dos.CouponDO;
import com.tarena.lbs.pojo.marketing.param.CouponParam;
import com.tarena.lbs.pojo.marketing.param.UpdateParam;
import com.tarena.lbs.pojo.marketing.query.CouponQuery;
import java.util.Date;
import java.util.List;
import java.util.stream.Collectors;
import javax.annotation.Resource;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

@Service
public class CouponRepositoryImpl implements CouponRepository {

    @Resource
    private CouponConverter couponConverter;

    @Resource
    private CouponMapper couponMapper;

    @Resource
    private CouponCodeRepository couponCodeRepository;

    @Override
    @Transactional(rollbackFor = Exception.class)
    public CouponDO insert(CouponParam couponParam) throws BusinessException {
        CouponDO couponDO = couponConverter.param2DO(couponParam);
        couponMapper.insert(couponDO);
        couponCodeRepository.batchInsertCouponCode(couponDO.getId(),couponParam.getBusinessId(),couponParam.getMaxUsageLimit());
        return couponDO;
    }

    @Override
    public void update(CouponParam couponParam) {
        CouponDO couponDO = new CouponDO();
        BeanUtils.copyProperties(couponParam,couponDO);
        couponDO.setUpdateAt(new Date());
        couponMapper.updateByPrimaryKeySelective(couponDO);
    }

    @Override
    public void updateStatus(UpdateParam updateParam) {
        CouponDO couponDO = new CouponDO();
        couponDO.setId(updateParam.getId());
        couponDO.setStatus(updateParam.getStatus());
        couponDO.setUpdateAt(new Date());
        couponMapper.updateByPrimaryKeySelective(couponDO);
    }

    @Override
    public CouponBO getDetail(Integer couponId) {
        CouponDO couponDO = couponMapper.selectByPrimaryKey(couponId);
        return couponConverter.do2bo(couponDO);
    }

    @Override
    public BasePageList<CouponBO> list(CouponQuery couponQuery) {
        int page = couponQuery.getPageNo();
        int pageSize = couponQuery.getPageSize();
        //new PageQuery(page, pageSize);
        List<CouponDO> list = couponMapper.selectList(couponQuery);
        PageInfo<CouponDO> pageInfo = new PageInfo<>(list);
        List<CouponBO> couponBOList = pageInfo.getList().stream().map(storeDO -> {
            CouponBO storeBO = new CouponBO();
            BeanUtils.copyProperties(storeDO, storeBO);
            return storeBO;
        }).collect(Collectors.toList());
        return new BasePageList<CouponBO>(couponBOList,Long.valueOf(pageInfo.getTotal()));
    }
}
